<?php

namespace App\Http\Controllers\Common;

use App\Model\Common\Email;
use App\Model\Common\User;
use Illuminate\Http\Request;
use Validator;
use Illuminate\Support\Facades\Cache;

class ToolController extends CommonController
{
    public function captcha()
    {
        return $this->output(0, '', ['captcha_src' => captcha_src()]);
    }

    public function emailCaptcha(Request $request)
    {
        $cfgs = [
            '1' => ['register', -1],
            '2' => ['login', 0],
            '3' => ['findpassword', 1],
            '4' => ['oldemail', 1],
            '5' => ['newemail', -1],
        ];
        $type = $request->input('code_type', 0);

        if (!isset($cfgs[$type])) {
            return $this->output(10005);
        }
        $cfg = $cfgs[$type];
        $email = $request->input('email', '');
        if (!$email || !is_email($email)) {
            return $this->output(20006);
        }
        if (!$request->has('captcha')) {
            return $this->output(20010);
        }

        $a = captcha_check($request->input('captcha', ''));
        // var_dump($a);
        if (!$a) {
            return $this->output(20011);
        }

        $User = new User();
        $ex = $User->checkEmailExist($email);
        if ($cfg[1] === -1 && $ex) {
            return $this->output(20012);
        }
        if (1 === $cfg[1] && !$ex) {
            return $this->output(20013);
        }

        $last = session('email_last');

        if ($last) {
            list($s_email, $s_time) = explode('|', $last);
            if (time() - $s_time < 60) {
                return $this->output(10010);
            }
        }

        $code = make_email_verfiy_token($email);
        $Mail = new Email();
        $response = $Mail->sendMail(
            $cfg[0], // 邮件模板
            $email, // 邮件接收者
            [
                'code' => $code,
                ]
            );
        if (false === $response) {
            return $this->output(10001, $Mail->getError());
        }

        session(['email_last' => $email.'|'.time()]);

        return $this->output(0);
    }

    public function clearCache()
    {
        Cache::flush();

        return $this->output(0);
    }

    public function uploadImg(Request $request)
    {
        $data = $request->all();

        $validator = Validator::make($data, [
            'img' => 'required|file|image|max:4096',
        ]);

        if ($validator->fails()) {
            return $this->output(10005, $validator->errors());
        }

        $temp = $request->input('temp', 0);

        // 获取表单上传文件
        $file = $data['img'];

        if ($temp)
        {
            $path = $file->store('temp/'.date('Ymd'));   
        }
        else
        {
            $path = $file->store(date('Ymd'));   
        }
        // $files = $request->file('imgs');
        if ($path) {
            return $this->output(0, '', ['url' => env('APP_UPLOAD').$path, 'path' => $path]);
        } else {
            return $this->output(10005, '文件上传失败');
        }
    }
}
